package com.gw.vip.server.controller;

import com.gw.vip.common.log.PrintlnLog;
import com.gw.vip.common.vo.Result;
import com.gw.vip.server.dto.*;
import com.gw.vip.server.sign.RequiredSign;
import com.gw.vip.server.vo.*;
import com.gw.vip.server.service.AuthBiz;
import com.gw.vip.server.service.MainService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author GW
 * @description 授权后交易
 * @since 2022-06-06
 */
@RestController
@RequestMapping("authTrans")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AuthTransController {

    private final AuthBiz authBiz;
    private final MainService mainService;

    @PrintlnLog(description = "根据用户码消费")
    @PostMapping("saleByCode")
    @RequiredSign
    public Result saleByCode(@RequestBody @Valid SaleByCodeDto dto){
        //获取用户信息
        UserInfoByCodeDto userInfoByCodeDto = new UserInfoByCodeDto();
        userInfoByCodeDto.setRandomCode(dto.getRandomCode());
        UserInfoByCodeVo userInfoByCodeVo = authBiz.userInfoByCode(userInfoByCodeDto);

        //支付
        SaleDto saleDto = new SaleDto();
        BeanUtils.copyProperties(dto, saleDto);
        saleDto.setUserId(userInfoByCodeVo.getId());
        MainAccResponse saleVo = mainService.sale(saleDto);

        SaleByCodeVo vo = new SaleByCodeVo();
        vo.setUserInfo(userInfoByCodeVo);
        vo.setSaleInfo(saleVo);
        return Result.success(vo);
    }

    @PrintlnLog(description = "根据用户码充值")
    @PostMapping("chargeByCode")
    @RequiredSign
    public Result chargeByCode(@RequestBody @Valid ChargeByCodeDto dto){
        //获取用户信息
        UserInfoByCodeDto userInfoByCodeDto = new UserInfoByCodeDto();
        userInfoByCodeDto.setRandomCode(dto.getRandomCode());
        UserInfoByCodeVo userInfoByCodeVo = authBiz.userInfoByCode(userInfoByCodeDto);

        //充值
        ChargeDto chargeDto = new ChargeDto();
        BeanUtils.copyProperties(dto, chargeDto);
        chargeDto.setUserId(userInfoByCodeVo.getId());
        MainAccResponse mainAccResponse = mainService.charge(chargeDto);

        ChargeByCodeVo vo = new ChargeByCodeVo();
        vo.setUserInfo(userInfoByCodeVo);
        vo.setChargeInfo(mainAccResponse);
        return Result.success(vo);
    }

    @PrintlnLog(description = "根据用户码查询余额")
    @PostMapping("amtQueryByCode")
    @RequiredSign
    public Result amtQueryByCode(@RequestBody @Valid AmtQueryByCodeDto dto){
        //获取用户信息
        UserInfoByCodeDto userInfoByCodeDto = new UserInfoByCodeDto();
        userInfoByCodeDto.setRandomCode(dto.getRandomCode());
        UserInfoByCodeVo userInfoByCodeVo = authBiz.userInfoByCode(userInfoByCodeDto);

        //查询余额
        AmtQueryDto amtQueryDto = new AmtQueryDto();
        amtQueryDto.setShopId(dto.getShopId());
        amtQueryDto.setUserId(userInfoByCodeVo.getId());
        AmtQueryVo amtQueryVo = mainService.amtQuery(amtQueryDto);

        AmtQueryByCodeVo vo = new AmtQueryByCodeVo();
        vo.setUserInfo(userInfoByCodeVo);
        vo.setAmtQueryVo(amtQueryVo);
        return Result.success(vo);
    }
}
